﻿
using ZFY.Blog.Api.Model.Dto.ArticleDto;
using ZFY.Blog.Api.Model.Model;
using ZFY.Blog.Api.Services;
using AutoMapper;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using ZFY.AspNetCore;
using ZFY.Blog.Api.IRepository;
using ZFY.Core;

namespace ZFY.Blog.Api.AutoMapper.ArticleResolver
{
    public class UpdateArticleUrlResolver : IValueResolver<UpdateArticleInput, Article, string>
    {
        private readonly IArticleRepository _articleRepository;

        public UpdateArticleUrlResolver(IArticleRepository articleRepository)
        {
            _articleRepository = articleRepository;
        }
        public string Resolve(UpdateArticleInput source, Article destination, string destMember, ResolutionContext context)
        {
            var article = AsyncHelper.RunSync(()=>_articleRepository.SingleAsync(x => x.Id.Equals(destination.Id)));
            File.WriteAllText(StaticInfo.WebRootPath + "/Article/" + article.Url, source.Body);
            return article.Url;
        }
    }
}
